﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MyDBconnection;


namespace Thesis_2
{
    public partial class frmCustomerList : Form
    {
        DatabaseConnection dbconn = new DatabaseConnection();
        public frmCustomerList()
        {
            InitializeComponent();
            this.Load += new EventHandler(frmCustomerList_Load);
            dataGridView1.RowPostPaint += new DataGridViewRowPostPaintEventHandler(dataGridView1_RowPostPaint);
            txtsearch.TextChanged += new EventHandler(txtsearch_TextChanged);
            dataGridView1.RowHeaderMouseClick += new DataGridViewCellMouseEventHandler(dataGridView1_RowHeaderMouseClick);
           
        }
        void dataGridView1_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            DataGridViewRow dr = dataGridView1.SelectedRows[0];
            this.Hide();

        }
        void txtsearch_TextChanged(object sender, EventArgs e)
        {
            if (txtsearch.Text != "Search Customer")
            {
                dbconn.dbconnection();
                dbconn.loadData("SELECT distinct CustomerID,Customername,Address,City,Contactno,AltContactno,Email,DateAdded FROM tblcustomerprofile WHERE Customername LIKE '%" + txtsearch.Text + "%'");
                dataGridView1.DataSource = dbconn.tbl;
                dbconn.DBClose();
            }
            else
            {
                GetData();
            }
        }
        void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            string strRowNumber = (e.RowIndex + 1).ToString();
            SizeF size = e.Graphics.MeasureString(strRowNumber, this.Font);
            if (dataGridView1.RowHeadersWidth < Convert.ToInt32((size.Width + 20)))
            {
                dataGridView1.RowHeadersWidth = Convert.ToInt32((size.Width + 20));
            }
            Brush b = SystemBrushes.ControlText;
            e.Graphics.DrawString(strRowNumber, this.Font, b, e.RowBounds.Location.X + 15, e.RowBounds.Location.Y + ((e.RowBounds.Height - size.Height) / 2));
        }
        void frmCustomerList_Load(object sender, EventArgs e)
        {
            GetData();
            dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
            dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
        }
        void GetData()
        {
            dbconn.dbconnection();
            dbconn.loadData("Select distinct CustomerID,Customername,Address,City,Contactno,AltContactno,Email,DateAdded from tblcustomerprofile");
            dataGridView1.DataSource = dbconn.tbl;
            dbconn.DBClose();

        }

    }
}
